package com.tsinghuabigdata.edu.ddmath.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tsinghuabigdata.edu.ddmath.R;

import static com.tsinghuabigdata.edu.ddmath.R.id.no_data_txt;

public class LoadFaultView extends RelativeLayout implements View.OnClickListener {

    private LinearLayout mFaultLayout;
    private Button       mBtnRetry;
    private LinearLayout mEmptyLayout;
    private ImageView    mIvEmpty;
    private TextView     mTvEmpty;

    private int emptyImgResid;

    private View mainView;

    private boolean isEmpty;
    private String emptyText = "什么数据都没有发现";

    private OnClickListener mListener;

    public LoadFaultView(Context context) {
        super(context);
        initialize();
    }

    public LoadFaultView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public LoadFaultView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    /**
     * 初始化界面元素
     */
    private void initialize() {
        mainView = inflate(getContext(), R.layout.layout_load_fault, this);

        mFaultLayout = (LinearLayout) findViewById(R.id.fault_layout);
        mBtnRetry = (Button) findViewById(R.id.btn_retry);
        mEmptyLayout = (LinearLayout) findViewById(R.id.empty_layout);
        mIvEmpty = (ImageView) findViewById(R.id.iv_empty);
        mTvEmpty = (TextView) findViewById(R.id.tv_empty);

        mBtnRetry.setOnClickListener(this);
    }

    public void setEmptyIcon(int resid) {
        emptyImgResid = resid;
        mIvEmpty.setImageResource(resid);
    }

    public void setEmptyText(String text) {
        emptyText = text;
    }

    public void show(boolean isShowEmptyLayout) {
        setVisibility(VISIBLE);
        if (isShowEmptyLayout) {
            mFaultLayout.setVisibility(GONE);
            mEmptyLayout.setVisibility(VISIBLE);
            mTvEmpty.setText(emptyText);
            if (emptyImgResid == 0) {
                mIvEmpty.setVisibility(GONE);
            } else {
                mIvEmpty.setVisibility(VISIBLE);
            }
        } else {
            mFaultLayout.setVisibility(VISIBLE);
            mEmptyLayout.setVisibility(GONE);
        }
    }

    public void hide() {
        setVisibility(VISIBLE);
        mFaultLayout.setVisibility(GONE);
        mEmptyLayout.setVisibility(GONE);
    }

    /**
     * 绑定点击事件
     *
     * @param listener
     */
    public void setListener(OnClickListener listener) {
        this.mListener = listener;
    }

    public OnClickListener getmListener() {
        return mListener;
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            mListener.onClick(v);
        }
    }
}
